package it.polito.connectedcar.message;

import it.polito.connectedcar.tcc.zone.Token;
import it.polito.connectedcar.util.Constants;

/**
 * 
 /** This message is used for vehicle to vehicle communication. Vehicles
 * exchange their position,speed and direction in order to preemptively alert
 * about possible collisions. In order for a message coming from a vehicle , to
 * be accepted by the receiving vehicle, 2 conditions must apply :
 * <ol>
 * <li>the source address of the sender must be known by the receiver in some of
 * the current managed tokens</li>
 * <li>the token used by the sender to identity its belonging group must be the
 * same as the one known by the receiver</li>
 * </ol>
 * If one of these conditions does not apply , the message has to dropped
 * 
 * @author Nicola Aresta
 * 
 */
public class VehicleToVehicleMessage extends OutgoingMessage {

	private Double speed;
	private Double direction;
	private Double utmXPos;
	private Double utmYPos;
	private String currentLaneId;
	private Token belongingToken;
	private Token vehicleIdentifier;

	public VehicleToVehicleMessage(byte messageCode, Token vehicleIdentifier) {
		super(Constants.VEHICLE_TYPE_CODE, messageCode);
		this.vehicleIdentifier = vehicleIdentifier;
	}

	@Override
	protected void convertToData(Storage st, boolean forTCC, Token t) {

		/* write the belonging token */
		if (forTCC)
			for (int i = 0; i < belongingToken.getBytes().length; i++) {
				st.writeByte(belongingToken.getBytes()[i]);
			}

		/* write the zone token */
		for (int i = 0; i < t.getBytes().length; i++)
			st.writeByte(t.getBytes()[i]);

		st.writeStringASCII(vehicleIdentifier.toString());
		st.writeDouble(utmXPos);
		st.writeDouble(utmYPos);
		st.writeDouble(direction);
		st.writeDouble(speed);
		st.writeStringASCII(currentLaneId);
	}

	public Double getSpeed() {
		return speed;
	}

	public void setSpeed(Double speed) {
		this.speed = speed;
	}

	public Double getDirection() {
		return direction;
	}

	public void setDirection(Double direction) {
		this.direction = direction;
	}

	public Double getUtmXPos() {
		return utmXPos;
	}

	public void setUtmXPos(Double utmXPos) {
		this.utmXPos = utmXPos;
	}

	public Double getUtmYPos() {
		return utmYPos;
	}

	public void setUtmYPos(Double utmYPos) {
		this.utmYPos = utmYPos;
	}

	public Token getBelongingToken() {
		return belongingToken;
	}

	public void setBelongingToken(Token belongingToken) {
		this.belongingToken = belongingToken;
	}

	public String getCurrentLaneId() {
		return currentLaneId;
	}

	public void setCurrentLaneId(String currentLaneId) {
		this.currentLaneId = currentLaneId;
	}

	public Token getVehicleIdentifier() {
		return vehicleIdentifier;
	}

	public void setVehicleIdentifier(Token vehicleIdentifier) {
		this.vehicleIdentifier = vehicleIdentifier;
	}

}
